package pl.lodz.p.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

import pl.lodz.p.model.enums.DayOfTheWeek;
import pl.lodz.p.model.enums.EnumBean;
import pl.lodz.p.model.enums.Gender;
import pl.lodz.p.model.enums.Specialization;

@Entity
public class Doctor {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	@Column(name="firstName", nullable=true)
	private String firstName;

	@Column(name="middleName")
	private String middleName;

	@Column(name="lastName", nullable=true)
	private String lastName;

	@Column(nullable=true)
	private String pesel;
	
	@Column(nullable=true)
	private String nip;
	
	@Column(nullable=true)
	private Date birthDate;
	
	@Enumerated(EnumType.STRING)
	private Gender gender;
	
	@Column(nullable=true)
	private String mobilePhone;

	@Column(nullable=true)
	private String email;
	
	@Embedded
    private Address address;

	@Enumerated(EnumType.STRING)
	private Specialization spec;
	
	@OneToMany(mappedBy="doctor", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	private List<Schedule> schedules;
	
	@ManyToOne
	@PrimaryKeyJoinColumn
	private User user;
	
	@OneToMany(mappedBy="doctor", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	private Set<WorkingDay> workingDays = new TreeSet<WorkingDay>();
	
	private int visit;
	
	public static final int MINUTE = 1000*60;
	
	public Doctor() {
		super();
		address = new Address();
		schedules = new ArrayList<Schedule>();
		EnumBean eB = new EnumBean();
		for (DayOfTheWeek day : eB.getDayOfTheWeek()) {
			WorkingDay workingDay = new WorkingDay();
			workingDay.setDayOfTheWeek(day);
			workingDay.setStartHour(7);
			workingDay.setEndHour(21);
			workingDay.setDoctor(this);
			getWorkingDays().add(workingDay);
		}
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPesel() {
		return pesel;
	}

	public void setPesel(String pesel) {
		this.pesel = pesel;
	}

	public String getNip() {
		return nip;
	}

	public void setNip(String nip) {
		this.nip = nip;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return getId() + "";
	}

	public Specialization getSpec() {
		return spec;
	}

	public void setSpec(Specialization spec) {
		this.spec = spec;
	}

	public List<Schedule> getSchedules() {
		return schedules;
	}

	public void setSchedules(List<Schedule> schedules) {
		this.schedules = schedules;
	}
	
	@SuppressWarnings("deprecation")
	public int getAge() {
		return new Date().getYear()-birthDate.getYear();
	}

	public String getFullName(){
		return firstName + " " + lastName;
	}

	public String getFull(){
		return firstName + " " + lastName + " (" + spec.getLabel() + ")";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Set<WorkingDay> getWorkingDays() {
		TreeSet set = new TreeSet<WorkingDay>();
		set.addAll(workingDays);
		return set;
	}

	public void setWorkingDays(Set<WorkingDay> workingDays) {
		this.workingDays = workingDays;
	}

	public int getVisit() {
		return visit;
	}

	public void setVisit(int visit) {
		this.visit = visit;
	}

}
